package com.tmxk.api.days01.yc.list;

import com.tmxk.api.days01.yc.Dog;

import java.util.ArrayList;

/**
 * @description: ArrayList动态数组集合
 * @author: yc
 * @date: 2022/11/28 13:33
 * @Version: 1.0
 **/
public class ArrayListDemo_Generic {
    public static void main(String[] args) {
        ArrayList<Dog> arrayList = new ArrayList<>();
        Dog dog1 = new Dog("小花",2);
        arrayList.add(dog1);
        arrayList.add(new Dog("花花",3));
        arrayList.add(new Dog("小黑",1));
        arrayList.add(new Dog("小白",5));
        arrayList.add(new Dog("汪汪",6));

        //获取第三个下标元素的狗狗对象
        Dog dog = arrayList.get(2);
        System.out.println("获取第三个下标元素的狗狗对象:" + dog);
        //
        boolean flag = true;
        for (Dog d : arrayList) {
            if("汪汪".equals(dog.getName())){
                flag = true;
                break;
            }
        }
        System.out.println(flag == true ? "包含在该集合元素中" : "不包含在该集合元素中");
        //删除花花
        boolean isRemove = false;
        for (int i = 0; i < arrayList.size(); i++) {
            Dog d = arrayList.get(i);
            if("花花".equals(d.getName())){
                isRemove = arrayList.remove(d);
            }
        }
        System.out.println(isRemove == true ? "删除花花成功" : "删除花花失败");
        //循环遍历集合元素
        System.out.println("\n循环遍历该集合元素");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i).getName() + "\t" + arrayList.get(i).getAge());
        }
    }
}
